﻿/***
    This file is part of Qasparov.

    Qasparov is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Qasparov is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Qasparov. If not, see <http://www.gnu.org/licenses/>.

 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
using Core = Com.Oxilly.Qsb.Core;
using SDK = Com.Oxilly.Qsb.Core.SDK;


namespace Com.Oxilly.Qsb.Services
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] 
    public class QsbCommandService : IQsbCommandService
    {
        private static List<IRemoteQasparovClient> callbackList = new List<IRemoteQasparovClient>();

        private Core.Engine Engine { get; set; }

        private SDK.QsbEngineApi Api { get { return this.Engine.Api; } }

        public void AttacheClient()
        {
            var callback = OperationContext.Current.GetCallbackChannel<IRemoteQasparovClient>();
            if(!callbackList.Contains(callback))
            {
                callbackList.Add(callback);
            }
        }

        public void DetacheClient()
        {    
            var callback = OperationContext.Current.GetCallbackChannel<IRemoteQasparovClient>();
            if(callbackList.Contains(callback))
            {
                callbackList.Remove(callback);
            }
 
        }

        #region API Methodes

        public void RestartEngine()
        {
            Api.RestartEngine();
        }


        public void InjectQsbMessage(QsbMessageWrapper msg)
        {
            this.Api.ProcessQsbMessage(msg.Message);
        }
        
        #endregion

        public void SetEngine(Core.Engine engine)
        {
            this.Engine = engine;

            engine.OnQsbMessagePublished += new EventHandler<SDK.Events.QsbMessagePublished>(engine_OnQsbMessagePublished);
        }

        void engine_OnQsbMessagePublished(object sender, SDK.Events.QsbMessagePublished e)
        {
             List<IRemoteQasparovClient> markToRemove = null;
           
            foreach (var remoteClient in callbackList)
            {
                try
                {
                    remoteClient.onMessagePublished(e.Message);
                }
                catch 
                {
                    if (markToRemove == null) {
                        markToRemove = new List<IRemoteQasparovClient>();
                    }
                    markToRemove.Add(remoteClient);
                }
            }
            if (markToRemove != null)
            {
                foreach (var h in markToRemove)
                {
                    callbackList.Remove(h);
                }
            }
        }
    }
}
